from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
from webdriver_manager.microsoft import EdgeChromiumDriverManager

# 初始化 driver 为 None
driver = None

try:
    # 自动下载并配置与当前 Edge 浏览器版本匹配的 EdgeDriver
    chromeDriver = webdriver.Chrome()

    # 打开百度
    chromeDriver.get('https://www.baidu.com')

    # 使用显式等待确保搜索框加载完成
    wait = WebDriverWait(chromeDriver, 10)
    search_box = wait.until(EC.presence_of_element_located((By.ID, 'kw')))

    # 在搜索框中输入python
    search_box.send_keys('python')

    # 模拟按下回车键进行搜索
    search_box.submit()
    time.sleep(10)
    # 可以根据需要添加更多操作

except TimeoutException:
    print("页面加载超时")
except NoSuchElementException:
    print("找不到元素")
except Exception as e:
    print(f"发生其他异常: {e}")


